package com.shiguiwu.springmybatis.spring.aop.principle;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @description: 使用jdk动态代理
 * @author: stone
 * @date: Created by 2021/6/9 15:17
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.principle
 */
public class AopJdkTests {

    public static interface IService {
        void m1(int i);
    }

    public static class Service  implements IService{
      public  void m1(int i) {
            System.out.println("iiiiiiiiii==> " + i);
        }
    }

    public static void main(String[] args) {

        ProxyFactory proxyFactory = new ProxyFactory(new Service());

        proxyFactory.setInterfaces(IService.class);

        proxyFactory.addAdvice(new MethodBeforeAdvice() {

            @Override
            public void before(Method method, Object[] objects, Object o) throws Throwable {
                System.out.println("haha");

            }
        });

        Object proxy = proxyFactory.getProxy();
        IService iService = (IService) proxy;

        iService.m1(1);

        System.out.println(iService.getClass());
        Class<?> superclass = iService.getClass().getSuperclass();
        System.out.println(superclass.getName());

        Class<?>[] interfaces = iService.getClass().getInterfaces();
        Arrays.stream(interfaces).forEach(System.out::println);
    }
}
